﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Learning
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Learning;
    public partial class CreateProfile : LearningPageBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (LearningOperations.GetUserLearningProfile(Profile.UserId).Count >= 3)
            {
                SetTopMostMessage("最多可以创建3条互助信息。", true);
                RedirectToDefaultPage();
            }
            else
            {
                Wizard1.ActiveStepChanged += new EventHandler(Wizard1_ActiveStepChanged);
                if (Session["LEARNING_PROFILE"] == null || !IsPostBack)
                {
                    Session["LEARNING_PROFILE"] = null;
                    Session["LEARNING_PROFILE"] = new Sc_LearningProfile();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["LAST_STEP"] = 0;
                txtComment.Text = SiteConfig.ReadSetting(SiteConfigSection.Learning, "DefaultProfileComment"); 
            }
        }

        void Wizard1_ActiveStepChanged(object sender, EventArgs e)
        {
            if (Session["LAST_STEP"] == null || (int)Session["LAST_STEP"] != Wizard1.ActiveStepIndex - 1)
            {
                Session["LAST_STEP"] = null;
                Response.Redirect("./");
            }
            else
            {
                switch (Wizard1.ActiveStepIndex)
                {
                    case 0:
                        break;
                    case 1:
                        SaveSkillData();
                        break;
                    case 2:
                        SaveDateData();
                        break;
                    case 3:
                        SaveStep3Data();
                        break;
                    default: break;
                }
                Session["LAST_STEP"] = Wizard1.ActiveStepIndex;
            }
            
        }

        private void SaveStep3Data()
        {
            Sc_LearningProfile lp = LoadProfileFromMemory();
            lp.Comment = txtComment.Text;     
            //save learning profile
            dataContext.Sc_LearningProfiles.InsertOnSubmit(lp);
            dataContext.SubmitChanges();
        }

        private void SaveSkillData()
        {
            Sc_LearningProfile lp = LoadProfileFromMemory();
            if (Category1.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level1.SelectedValue);
                io.Skill = Request.Form[this.Skill1.UniqueID];
                io.Direction = LearningSkillDirection.Output;
                lp.Sc_LearningIOs.Add(io);
            }

            if (Category2.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level2.SelectedValue);
                io.Skill = Request.Form[this.Skill2.UniqueID];
                io.Direction = LearningSkillDirection.Output;
                lp.Sc_LearningIOs.Add(io);
            }

            if (Category3.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level3.SelectedValue);
                io.Skill = Request.Form[this.Skill3.UniqueID];
                io.Direction = LearningSkillDirection.Output;
                lp.Sc_LearningIOs.Add(io);
            }

            if (Category4.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level4.SelectedValue);
                io.Skill = Request.Form[this.Skill4.UniqueID];
                io.Direction = LearningSkillDirection.Input;
                lp.Sc_LearningIOs.Add(io);
            }

            if (Category5.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level5.SelectedValue);
                io.Skill = Request.Form[this.Skill5.UniqueID];
                io.Direction = LearningSkillDirection.Input;
                lp.Sc_LearningIOs.Add(io);
            }

            if (Category6.SelectedValue != "")
            {
                Sc_LearningIO io = new Sc_LearningIO();
                io.LearningIoId = Guid.NewGuid();
                io.LearningProfileId = lp.LearningProfileId;
                io.Level = int.Parse(Level6.SelectedValue);
                io.Skill = Request.Form[this.Skill6.UniqueID];
                io.Direction = LearningSkillDirection.Input;
                lp.Sc_LearningIOs.Add(io);
            }
        }

        private void SaveDateData()
        {
            Sc_LearningProfile lp = LoadProfileFromMemory();
            lp.UserId = Profile.UserId;

            try
            {
                lp.StartDate = Convert.ToDateTime(txtStartDate.Text);
                lp.EndDate = Convert.ToDateTime(txtEndDate.Text);
            }
            catch { lp.StartDate = null; lp.EndDate = null; }

            lp.Status = LearningProfileStatus.Active;
            lp.CreateDate = DateTime.Now;
            lp.LearningProfileId = Guid.NewGuid();
            lp.Enabled = true;

            char[] timeSep = {'|'};
            foreach (ListItem item in chkTimeSpanInDay.Items)
            {
                if (item.Selected)
                {
                    if (lp.Sc_LearningTimeSpans.Count(d => d.Comment == item.Text) == 0)
                    {
                        string[] t = item.Value.Split(timeSep);
                        Sc_LearningTimeSpan s = new Sc_LearningTimeSpan();
                        s.StartTime = Convert.ToDateTime(t[0]);
                        s.EndTime = Convert.ToDateTime(t[1]);
                        s.Comment = item.Text;
                        s.LearningProfileId = lp.LearningProfileId;
                        s.LearningTimeId = Guid.NewGuid();
                        lp.Sc_LearningTimeSpans.Add(s);
                    }
                }
            }

            List<string> t1 = new List<string>();
            foreach (ListItem item in chkDaysInWeek.Items)
            {
                if (item.Selected)
                {
                    t1.Add(item.Value);
                }
            }

            lp.DaysInWeek = string.Join("|", t1.ToArray());

            SaveProfileToMemory(lp);
        }

        Sc_LearningProfile LoadProfileFromMemory()
        {
            return (Sc_LearningProfile)Session["LEARNING_PROFILE"];
        }

        void SaveProfileToMemory(Sc_LearningProfile p)
        {
            Session["LEARNING_PROFILE"] = p;
        }

        protected void AddOtherOption(object sender, EventArgs e)
        {
            DropDownList lst = (DropDownList)sender;
            lst.Items.Add(new ListItem("其他", "其他"));
            lst.Items.Add(new ListItem("-----", ""));
            lst.SelectedIndex = lst.Items.Count - 1;
        }
    }
}
